package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.WorkingMapper;
import com.entity.Working;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

@WebServlet(urlPatterns = { "/Per" }, name = "Per")
public class PerServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Logger logger = Logger.getLogger(PerServlet.class);

		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out=response.getWriter();
		SqlSession sqlSession = null;
		String pre = (request.getParameter("pre")==null? "list":request.getParameter("pre"));
		int count = 0;
		List<Working> list = new ArrayList<Working>();

		if (pre.equals("add")){
			Working working = new Working();
			working.setTitle(request.getParameter("title")) ;
			working.setContent(request.getParameter("content"));
			working.setType(Integer.parseInt(request.getParameter("type")));

			try {
				sqlSession=MybatisUtil.createSqlSession();
				count=sqlSession.getMapper(WorkingMapper.class).insert(working);

				sqlSession.commit();
			}catch (Exception e){
				e.printStackTrace();
				sqlSession.rollback();
				count=0;
			}finally {
				MybatisUtil.closeSqlSession(sqlSession);
			}
			if (count>0){
				out.print("<script>alert('添加成功！');location.href='Per?pre=list';</script>");
			}else {
				out.print("<script>alert('添加失败！');history.back();</script>");
			}
		}else if (pre.equals("up")){
			Working working = new Working();
			working.setId(Integer.parseInt(request.getParameter("id")));
			working.setTitle(request.getParameter("title")) ;
			working.setContent(request.getParameter("content"));
			working.setType(Integer.parseInt(request.getParameter("type")));

			try {
				sqlSession=MybatisUtil.createSqlSession();
				count=sqlSession.getMapper(WorkingMapper.class).update(working);

				sqlSession.commit();
			}catch (Exception e){
				e.printStackTrace();
				sqlSession.rollback();
				count=0;
			}finally {
				MybatisUtil.closeSqlSession(sqlSession);
			}
			if (count>0){
				out.print("<script>alert('修改成功！');location.href='Per?pre=list';</script>");
			}else {
				out.print("<script>alert('修改失败！');history.back();</script>");
			}
		}else if (pre.equals("del")){
			int id =Integer.parseInt( request.getParameter("id"));

			try {
				sqlSession=MybatisUtil.createSqlSession();
				count=sqlSession.getMapper(WorkingMapper.class).delete(id);

				sqlSession.commit();
			}catch (Exception e){
				e.printStackTrace();
				sqlSession.rollback();
				count=0;
			}finally {
				MybatisUtil.closeSqlSession(sqlSession);
			}
			if (count>0){
				out.print("<script>alert('删除成功！');location.href='Per?pre=list';</script>");
			}else {
				out.print("<script>alert('删除失败！');history.back();</script>");
			}
		}else if (pre.equals("list")){
			try {
				sqlSession=MybatisUtil.createSqlSession();
				list=sqlSession.getMapper(WorkingMapper.class).getAllWorking();
				logger.debug("list.size()"+list.size());
			}catch (Exception e){
				e.printStackTrace();
			}finally {
				MybatisUtil.closeSqlSession(sqlSession);
			}
			request.setAttribute("list", list);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else if (pre.equals("get")){
			Working working = null;
			int id =Integer.parseInt( request.getParameter("id"));
			try {
				sqlSession=MybatisUtil.createSqlSession();
				working=sqlSession.getMapper(WorkingMapper.class).getWorking(id);

			}catch (Exception e){
				e.printStackTrace();
			}finally {
				MybatisUtil.closeSqlSession(sqlSession);
			}
			request.setAttribute("w", working);
			request.getRequestDispatcher("get.jsp").forward(request, response);
		}


	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		doGet(req, resp);
	}
}
